import { defineStore } from "pinia";

import {
    getOutApi,
    getPutApi,
    exportOutApi,
    exportPutApi,
    exportOutInfoApi,
    getOutInfoApi
} from '@/Api/TestTube'
import { downloadFileData } from "@/utils/downloadFile";

import { useCommon } from "./Common";
const CommonStore = useCommon()

export const useTestTube = defineStore('TestTube', {
    state: (): TestTubeState => {
        return {
            loading: false,
            searchForm: {
                TableKey: 'out',
                serial: null,
                TimeArr: [],
                startData: '',
                endData: '',
                department: '',
                illArea: '',
                departmentWard: [],


                current: 1,
                pageNo: 1,
                pageSize: 20,
                showSizeChanger: true,
                total: 0,
                pageSizeOptions: ['20', '30', '50', '100'],
                showTotal: (total: number) => `共 ${total} 条`,
                onChange(pageNo: number, pageSize: number) {
                    this.current = pageNo
                    this.pageNo = pageNo
                    this.pageSize = pageSize
                }
            },
            exportForm: {
                TimeArr: [],
                startData: '',
                endData: '',
                isExportAll: false,

                department: '',
                illArea: ''
            },
            outTableData: [],
            putTableData: [],
            searchOutInfo: {
                outData: '',
                serial: '',
                outInfo: '',

                department: '',
                illArea: '',
                startData: '',
                endData: '',

                current: 1,
                pageNo: 1,
                pageSize: 50,
                total: 0,
                showTotal: (total: number) => `共 ${total} 条`,
                onChange(pageNo: number, pageSize: number) {
                    this.pageNo = pageNo
                    this.current = pageNo
                }
            },
            outInfoData: [],
        }
    },
    actions: {
        async getOutTableData() {
            this.loading = true
            if (this.searchForm.TimeArr && this.searchForm.TimeArr.length) {
                this.searchForm.startData = this.searchForm.TimeArr[0]
                this.searchForm.endData = `${this.searchForm.TimeArr[1]} 23:59:59`
            } else {
                this.searchForm.startData = ''
                this.searchForm.endData = ''
            }


            if (this.searchForm.departmentWard != undefined && this.searchForm.departmentWard.length == 1) {
                this.searchForm.department = this.searchForm.departmentWard[0]
                this.searchForm.illArea = null
            } else if (this.searchForm.departmentWard != undefined && this.searchForm.departmentWard.length == 2) {
                this.searchForm.department = this.searchForm.departmentWard[0]
                this.searchForm.illArea = this.searchForm.departmentWard[1]
            } else {
                if (CommonStore.department != '检验科') {
                    this.searchForm.department = CommonStore.department
                    this.searchForm.illArea = CommonStore.illArea
                } else {
                    this.searchForm.department = ''
                    this.searchForm.illArea = ''
                }
            }

            try {
                const data = await getOutApi(this.searchForm) as any
                if (data && data.length > 0) {
                    this.outTableData = data[0]
                    this.outTableData.forEach((item, index) => {
                        item.index = (this.searchForm.pageNo! - 1) * this.searchForm.pageSize! + index + 1
                    })
                    this.searchForm.total = data[1][0].total
                }
            } finally {
                this.loading = false
            }
        },
        async getPutTableData() {
            this.loading = true
            if (this.searchForm.TimeArr && this.searchForm.TimeArr.length) {
                this.searchForm.startData = this.searchForm.TimeArr[0]
                this.searchForm.endData = `${this.searchForm.TimeArr[1]} 23:59:59`
            } else {
                this.searchForm.startData = ''
                this.searchForm.endData = ''
            }

            if (this.searchForm.departmentWard != undefined && this.searchForm.departmentWard.length == 1) {
                this.searchForm.department = this.searchForm.departmentWard[0]
                this.searchForm.illArea = null
            } else if (this.searchForm.departmentWard != undefined && this.searchForm.departmentWard.length == 2) {
                this.searchForm.department = this.searchForm.departmentWard[0]
                this.searchForm.illArea = this.searchForm.departmentWard[1]
            } else {
                if (CommonStore.department != '检验科') {
                    this.searchForm.department = CommonStore.department
                    this.searchForm.illArea = CommonStore.illArea
                } else {
                    this.searchForm.department = ''
                    this.searchForm.illArea = ''
                }
            }
            try {
                const data = await getPutApi(this.searchForm) as any
                if (data && data.length > 0) {
                    this.putTableData = data[0]
                    this.putTableData.forEach((item, index) => {
                        item.index = (this.searchForm.pageNo! - 1) * this.searchForm.pageSize! + index + 1
                        if (item.illArea) {
                            item.index = (this.searchForm.pageNo! - 1) * this.searchForm.pageSize! + index + 1
                        }
                    })
                    this.searchForm.total = data[1][0].total
                }
            } finally {
                this.loading = false
            }
        },
        async exportOutTableData() {
            if (this.exportForm.TimeArr && this.exportForm.TimeArr.length) {
                this.exportForm.startData = this.exportForm.TimeArr[0]
                this.exportForm.endData = `${this.exportForm.TimeArr[1]} 23:59:59`
            } else {
                this.exportForm.startData = ''
                this.exportForm.endData = ''
            }

            this.exportForm.department = CommonStore.department == '检验科' ? '' : CommonStore.department
            this.exportForm.illArea = CommonStore.illArea


            const data = await exportOutApi(this.exportForm)

            if (data) {
                downloadFileData(data, '取样记录.xlsx')
                this.exportForm.isExportAll = false
            }
        },
        async exportPutTableData() {
            if (this.exportForm.TimeArr && this.exportForm.TimeArr.length) {
                this.exportForm.startData = this.exportForm.TimeArr[0]
                this.exportForm.endData = `${this.exportForm.TimeArr[1]} 23:59:59`
            } else {
                this.exportForm.startData = ''
                this.exportForm.endData = ''
            }

            this.exportForm.department = CommonStore.department == '检验科' ? '' : CommonStore.department
            this.exportForm.illArea = CommonStore.illArea

            const data = await exportPutApi(this.exportForm)
            if (data) {
                downloadFileData(data, '放样记录.xlsx')
            }
        },
        async getOutInfo(record: any) {
            this.searchOutInfo.serial = record.serial
            this.searchOutInfo.outData = record.outData
            this.searchOutInfo.outInfo = record.outInfo

            const data = await getOutInfoApi(this.searchOutInfo) as any

            if (data) {
                this.outInfoData = data[0]
                this.searchOutInfo.total = data[1][0].total
                this.outInfoData.forEach((item, index) => {
                    item.index = (this.searchOutInfo.pageNo! - 1) * this.searchOutInfo.pageSize! + index + 1
                })
            }
        },
        async exportOutInfo() {

            if (this.exportForm.TimeArr && this.exportForm.TimeArr.length) {
                this.searchOutInfo.startData = this.exportForm.TimeArr[0]
                this.searchOutInfo.endData = `${this.exportForm.TimeArr[1]} 23:59:59`
            } else {
                this.searchOutInfo.startData = ''
                this.searchOutInfo.endData = ''
            }


            if (CommonStore.department != '检验科') {
                this.searchOutInfo.department = CommonStore.department
                this.searchOutInfo.illArea = CommonStore.illArea
            } else {
                this.searchOutInfo.department = ''
                this.searchOutInfo.illArea = ''
            }


            const data = await exportOutInfoApi(this.searchOutInfo)

            if (data) {
                downloadFileData(data, '取样详情.xlsx')
                this.exportForm.isExportAll = false
            }
        }
    }
})
